package product;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CsChange extends JFrame {
	Connection con;
	String csNo;
	CodeList list;	
	
	Vector<String> col = new Vector<>();
	Vector<String> row;
	DefaultTableModel model;

	private JPanel contentPane;
	private JPanel panel;
	private JTextField textField;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Create the frame.
	 */
	public CsChange(Connection c, String csNm, CodeList list) {
		this.con = c;
		this.csNo = csNm;
		this.list = list;
		setTitle("거래처");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 334, 346);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		textField.setText(csNm);
		
		col.add("사업자번호");
		col.add("회사명");
		col.add("대표자명");
		col.add("주소");
		col.add("전화번호");
		col.add("팩스번호");
		model = new DefaultTableModel(col, 0);		
		
		// 거래처명으로 기본 한 건만 조회 세팅
		String sql = "SELECT * FROM CUSTOMER WHERE NAME =?";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql); 
			pstmt.setString(1, textField.getText());
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()){
				row = new Vector<>();
				row.add(rs.getString(1));
				row.add(rs.getString(2));
				row.add(rs.getString(3));
				row.add(rs.getString(4));
				row.add(rs.getString(5));
				row.add(rs.getString(6));
				model.addRow(row);
			}
			table.setModel(model);
			
			table.getTableHeader().setReorderingAllowed(false);
			table.getTableHeader().setResizingAllowed(false);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void search() { // 사업자 번호 or 회사명 or CEO 명으로 검색 가능
		model.setRowCount(0);
		String find = textField.getText();

		String sql = "SELECT * FROM CUSTOMER WHERE NUM LIKE '%" + find
				+ "%' or NAME LIKE '%" + find + "%' or CEO LIKE '%" + find
				+ "%' ORDER BY NUM";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();

			int columnCnt = rsmd.getColumnCount();

			while (rs.next()) {
				row = new Vector<>();
				row.add(rs.getString(1));
				row.add(rs.getString(2));
				row.add(rs.getString(3));
				row.add(rs.getString(4));
				row.add(rs.getString(5));
				row.add(rs.getString(6));
				model.addRow(row);
			}
			table.setModel(model);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTextField(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						search();
					}
				}
			});
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					 search();
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					int row = table.getSelectedRow();
					csNo = ""+table.getValueAt(row, 0); // 사업자번호
//					System.out.println(csNo);
//					list.csNo = csNo; // 클릭한 사업자 번호로 새로 셋팅
					String cs=""+table.getValueAt(row, 1); // 회사명
					System.out.println(cs);
					list.textField_8.setText(cs);
					CsChange.this.dispose();
				}
			});
		}
		return table;
	}
}
